package com.ruoyi.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.exception.user.Result;
import com.ruoyi.common.core.exception.user.StatusCode;
import com.ruoyi.user.entity.Reputation;
import com.ruoyi.user.entity.UserInformation;
import com.ruoyi.user.mapper.ReputationMapper;
import com.ruoyi.user.service.UserInformationService;
import com.ruoyi.user.mapper.UserInformationMapper;
import com.ruoyi.user.utils.BankUtil;
import com.ruoyi.user.utils.HttpClientUtil;
import com.ruoyi.user.utils.IdCardUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
* @author 14571
* @description 针对表【tb_user_information(用户信息表)】的数据库操作Service实现
* @createDate 2023-03-24 11:20:39
*/
@Service
public class UserInformationServiceImpl extends ServiceImpl<UserInformationMapper, UserInformation>
    implements UserInformationService{

    @Resource
    private ReputationMapper reputationMapper;

    @Override
    public Result saveInfomation(UserInformation userInformation) {
        //判断身份证号的正确性
        String checkIdNo = IdCardUtil.checkIdNo(userInformation.getRealname(), userInformation.getIdCard());
        if (!checkIdNo.equals("0000")){
            return Result.error(StatusCode.ERROR,checkIdNo);
        }
        //验证银行卡号
        if (!BankUtil.checkBank(userInformation.getBrandCard(), userInformation.getIdCard(), userInformation.getRealname(), userInformation.getMobile())){
            return Result.error(StatusCode.ACCESSERROR,"银行卡验证失败");
        }
        //用httpclient查询用户信誉分
        String findCredit = HttpClientUtil.doGet("http://localhost:9203/credit/findCredit?realname="+userInformation.getRealname()+"&idcard="+userInformation.getIdCard());
        userInformation.setCredit(Integer.valueOf(findCredit));
        //根据信誉分查询额度
        QueryWrapper<Reputation> wrapper = new QueryWrapper<>();
        wrapper.lt("reputation_min", findCredit);
        wrapper.gt("reputation_max", findCredit);
        Reputation reputation = reputationMapper.selectOne(wrapper);
        userInformation.setQuota(reputation.getQuota());
        try {
            //添加用户信息
            this.save(userInformation);
        } catch (Exception e) {

            e.printStackTrace();
            return Result.error(StatusCode.ERROR,"添加失败");
        }
        return Result.success();
    }
}




